package com.gxa.admin.recycling.service.impl;

import com.gxa.admin.recycling.mapper.ScoreMapper;
import com.gxa.admin.recycling.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author R
 * @data 2021-12-07 10:40
 */
@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    ScoreMapper mapper;

    @Override
    public Map<String,Object> getSign(String cycleStart,String cycleEnd,String fromTime,String toTime,String name) {
        Map<String,Object> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        String end = null;
        String start = null;
        end = new SimpleDateFormat("yyyy-MM").format(date);// 2021-12
//        System.out.println(end);
        String startYear = null;
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,-1);
        Date time = calendar.getTime();
        start = new SimpleDateFormat("yyyy-MM").format(time);// 2021-11
//        System.out.println(start);
        calendar.add(Calendar.MONTH,+2);
        Date time1 = calendar.getTime();
        String format = new SimpleDateFormat("yyyy-MM").format(time1);// 2022-01
        System.out.println(format);
        calendar.add(Calendar.MONTH,-2);
        map.put(end.substring(5,7),mapper.getSign(cycleStart,cycleEnd,fromTime,toTime,end+"-01 00:00:00",format+"-01 00:00:00",name));
        calendar.add(Calendar.YEAR,-1);
        String endYear = new SimpleDateFormat("yyyy").format(calendar.getTime());// 2020
//        System.out.println(endYear);
        calendar.add(Calendar.YEAR,+1);
//        int i = 2;
        String endTime = startYear+"-01";
        while (!endYear.equals(startYear)){
            map.put(start.substring(5,7),mapper.getSign(cycleStart,cycleEnd,fromTime,toTime,start+"-01 00:00:00",end+"-01 00:00:00",name));
            String index;
            end = start;
//            System.out.println(start);
            calendar.add(Calendar.MONTH,-1);
            start = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
//            System.out.println(end);
            startYear = new SimpleDateFormat("yyyy").format(calendar.getTime());// 2021
//            i++;
            if (start.equals(endTime)){
                map.put(start.substring(5,7),mapper.getSign(cycleStart,cycleEnd,fromTime,toTime,start+"-01 00:00:00",end+"-01 00:00:00",name));
            }
            System.out.println("endYear:"+endYear);
            System.out.println("startYear:"+startYear);
        }
        return map;
    }
}
